//
//  SISSHouseTypeViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/19.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSHouseTypeViewController.h"
#import "SISSComboBoxView.h"
#import "SISSHouseTypeCell.h"
#import "SISSProgressBottomView.h"
#import "SISSChatViewController.h"

@interface SISSHouseTypeViewController ()<SISSComboBoxViewDelegate,UITableViewDelegate,UITableViewDataSource,SISSProgressBottomViewDelegate>

/** 房源数组 */
@property (strong, nonatomic) NSArray *houseResourceArr;
/** 筛选视图 */
@property (weak, nonatomic) SISSComboBoxView *filterView;
/** tableView */
@property (weak, nonatomic) SISSBaseTableView *tableView;
/* bottomView */
@property (weak, nonatomic) SISSProgressBottomView *bottomView;

/** 户型 */
@property (copy, nonatomic) NSArray *houseType;
/** 价格 */
@property (copy, nonatomic) NSArray *price;
/** 面积 */
@property (copy, nonatomic) NSArray *area;
/** 建筑形式 */
@property (copy, nonatomic) NSArray *buildingType;
/** 状态 */
@property (copy, nonatomic) NSArray *statusType;
/** pageNo */
@property (assign, nonatomic) NSInteger pageNo;

@end

@implementation SISSHouseTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self requestFilterHouseTypeData];
}

#pragma mark - 代理
#pragma mark -- SISSComboBoxViewDelegate 筛选
- (void)comboBoxViewDidSelectDisplayTypeNormalWithRootItemTitle:(NSString *)rootItemTitle title:(NSString *)title {
    
    
    kSISSRefresh.refreshType = SISSRefressTypeUpDown;
    self.pageNo = 1;
//    [self requestFilterHouseTypeData];
    
}

- (void)comboBoxViewDidSelectDisplayTypeMultilayerWithRootItemTitle:(NSString *)rootItemTitle title:(NSString *)title subtitle:(NSString *)subtitle {
    
    
    kSISSRefresh.refreshType = SISSRefressTypeUpDown;
    self.pageNo = 1;
    [self requestFilterHouseTypeData];
    
}

- (void)comboBoxViewDidSelectDisplayTypeFiltersWithTitle:(NSString *)title subtitles:(NSArray *)subtitles {
    
    if ([title isEqualToString:@"户型"]) {
        self.houseType = @[];
        self.houseType = subtitles;
    } else if ([title isEqualToString:@"价格"]) {
        self.price = @[];
        self.price = subtitles;
    } else if ([title isEqualToString:@"面积"]) {
        self.area = @[];
        self.area = subtitles;
    } else if ([title isEqualToString:@"建筑形式"]) {
        self.buildingType = @[];
        self.buildingType = subtitles;
    } else if ([title isEqualToString:@"状态"]) {
        self.statusType = @[];
        self.statusType = subtitles;
    }
}

- (void)comboBoxViewDisplayTypeFiltersConfirmClickAction {
    
    self.pageNo = 1;
    [self requestFilterHouseTypeData];
    kSISSRefresh.refreshType = SISSRefressTypeUpDown;
    self.houseType = @[];
    self.price = @[];
    self.area = @[];
    self.buildingType = @[];
    self.statusType = @[];
}

#pragma mark -- UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    [tableView tableViewDisplayWithMessage:@"没有找到相关户型！" ifNecessaryForRowCount:self.houseResourceArr.count];
    if (self.houseResourceArr.count != 0) {
        return self.houseResourceArr.count;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSHouseTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:houseTypeCellReuseID forIndexPath:indexPath];
//
//    cell.houseResourceModel = self.houseResourceArr[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; // 去除tableView的选中状态
}

#pragma mark -- SISSProgressBottomViewDelegate 头像、电话、在线咨询
- (void)clickBottomButtonWithIndex:(NSInteger)index {
    
//    if (self.houseProgressInfoModel.businessStatus == 2 && index == 1) {
//        //        [self alertActionAlertWithTitle:@"提示" message:@"对方已开启拒绝接收您的请求"];
//        return;
//    }
    
    if (index == 0) { // 点击头像或名字的
        NSLog(@"人员信息");
        //        [kNetworkManager requestPersonInfoWithSpId:self.houseProgressModel.spId success:^(SISSPersonInfoModel *personInfo) {
        //
        //            SISSInfomationViewController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"SISSInfomationViewController"];
        //            vc.personInfo = personInfo;
        //
        //            vc.spId = self.houseProgressModel.spId;
        //            [self.navigationController pushViewController:vc animated:YES];
        //
        //        } failed:^(NSError *error) {
        //
        //            NSLog(@"%@",error);
        //        }];
        
    } else if (index == 1) { // 点击拨打电话
//        if (self.houseProgressInfoModel.progress < 3) {
//            [self alertActionAlertWithTitle:@"不能拨号" message:@"没有签署委托合同，暂不能拨号！"];
//        } else {
//            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"拨打电话" message:self.houseProgressModel.phoneNumber preferredStyle:UIAlertControllerStyleAlert];
//            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//
//            }];
//            UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//                UIWebView * callWebview = [[UIWebView alloc] init];
//                [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",self.houseProgressModel.phoneNumber]]]];
//                [[UIApplication sharedApplication].keyWindow addSubview:callWebview];
//            }];
//
//            [alertController addAction:cancelAction];
//            [alertController addAction:confirmAction];
//
//            [self presentViewController:alertController animated:YES completion:nil];
//        }
        
    } else { // 点击联系人
        
//        SISSChatViewController *chatVC = [[SISSChatViewController alloc] initWithConversationChatter:self.houseProgressInfoModel.imAccount conversationType:EMConversationTypeChat];
//        SISSContactModel *model = [[SISSContactModel alloc] init];
//        model.name = self.houseProgressModel.name;
//        model.accountType = 2;
//        //        model.spId = self.houseProgressModel.s
//        model.portrait = self.houseProgressInfoModel.portrait;
//        chatVC.contactModel = model;
//        chatVC.title = self.houseProgressModel.name;
//        [self.navigationController pushViewController:chatVC animated:YES];
    }
}


#pragma mark - 上下拉刷新加载方法
- (void)upDownLoadData {
    
    self.pageNo = 1;
    
    [self requestFilterHouseTypeData];
    
}

- (void)reloadMoreData {
    
    self.pageNo += 1;
    
    [self requestFilterHouseTypeData];
}



#pragma mark - 2.4.1 筛选房源、找房源的请求
- (void)requestFilterHouseTypeData {
    self.houseResourceArr = @[@"1",@"2",@"1",@"2"];
//    NSDictionary *parameters = @{
//                                 };
    //加载搜索跳转找房源的数据
//    WeakSelf
//    [kJsonRequestNetworkingManager requestFilterSearchHouseResourceWithPageNo:self.pageNo parameters:parameters success:^(SISSBasePagingModel *baseModel) {
//
//        NSArray *houseResources = [NSArray yy_modelArrayWithClass:[SISSHouseResourceModel class] json:baseModel.result];
//
//
//
//    } failed:^(NSError *error) {
//        [weakSelf.tableView.mj_header endRefreshing];
//        [weakSelf.tableView.mj_footer endRefreshing];
//        NSLog(@"%@",error);
//    }];
    
}


#pragma mark - 设置UI
- (void)setupSubviews {
    [super setupSubviews];
    
    // 添加筛选视图
    SISSComboBoxView *filterView = [[SISSComboBoxView alloc] initWithHouseTypeFrame:CGRectMake(0, NAVIGATION_HEIGHT, SCREEN_WIDTH, 44)];
    filterView.delegate = self;
    
    [self.view addSubview:filterView];
    self.filterView = filterView;
    
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    // 自动行高
    tableView.estimatedRowHeight = 80;
    tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }

    [tableView registerClass:[SISSHouseTypeCell class] forCellReuseIdentifier:houseTypeCellReuseID];
    
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    SISSProgressBottomView *bottomView = [[SISSProgressBottomView alloc] init];
    bottomView.delegate = self;
//    bottomView.name = self.houseProgressModel.name;
    [self.view addSubview:bottomView];
    self.bottomView = bottomView;
    
    // 自动布局
    CGFloat bottomViewHeight = 44.0;
    WeakSelf
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.filterView.mas_bottom);
        make.left.right.equalTo(weakSelf.view);
        make.bottom.mas_equalTo(-bottomViewHeight);
    }];
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(bottomViewHeight);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(weakSelf.view);
        }
    }];
}

@end
